<template>
  <article>
    <h1 class="t_nav">
      <a href="/" class="n1">网站首页</a>
      <a href="javascript:void(0);" class="n2">{{blog.blogSort.blorSortName}}</a>
    </h1>
    <div class="infosbox">
      <div class="newsview">
        <h3 class="news_title">{{blog.title}}</h3>
        <div class="bloginfo">
          <ul>
            <li class="author">
              <i class="el-icon-s-custom"></i>
              <a href="javascript:void(0);">{{blog.author}}</a>
            </li>
            <li class="lmname">
              <i class="el-icon-menu"></i>
              <a href="javascript:void(0);">{{blog.blogSort.blorSortName}}</a>
            </li>
            <li class="createTime">
              <i class="el-icon-date"></i>
              {{blog.createTime}}
            </li>
            <li class="view">
              <i class="el-icon-view"></i>
              {{blog.clickCount}}
            </li>
            <li class="like">
              <i class="el-icon-star-on"></i>
              {{blog.collectCount}}
            </li>
          </ul>
        </div>

        <div class="tags">
          <a href="javascript:void(0);" v-for="item in blog.tags" :key="item">{{item}}</a>
          <div class="news_about">
            <strong>版权</strong>
            版权声明
          </div>
          <div class="news_con ck-content" v-html="blog.content"></div>
        </div>

        <div class="news_pl">
          <h2>文章评论</h2>
          <ul>
            <!-- begin CommentBox-->
            <div>
              <div class="commentBox">
                <span class="left">
                  <img src="../../static/img/defaultAvatar.png" />
                </span>
                <span class="right">
                  <textarea
                    id="textpanel"
                    class="textArea"
                    placeholder="既然来了，那就留下些什么吧~"
                    v-model="value"
                    @input="vaildCount"
                  ></textarea>
                </span>
              </div>

              <div id="bottom">
                <el-button class="submit p2" type="primary" @click="handleSubmit()">发送评论</el-button>
                <el-button class="cancel p2" type="info">取消评论</el-button>
                <div class="emoji-panel-btn p2">
                  <img src="../../static/img/face_logo.png" />
                </div>
                <span class="allow p2">还能输入{{count}}个字符</span>
              </div>
            </div>

            <!-- CommentsList-->
            <div class="message_infos">
              <div>
                <div v-for="comment in blog.comments" :key="comment.uid">
                  <div class="commentList">
                    <span class="left p1">
                      <img src="../../static\img\touxiang.jpg" />
                    </span>

                    <span class="right p1">
                      <div class="rightTop">
                        <el-link class="userName" :underline="false">{{comment.user.nickName}}</el-link>
                        <!-- <el-tag style="height: 30px; margin-left:5px;"></el-tag> -->
                        <span class="timeAgo">{{(comment.createTime)}}</span>
                      </div>
                      <div class="rightCenter" v-html="comment.content"></div>
                      <div class="rightBottom">
                        <el-link class="b1" :underline="false" @click="replyTo(comment)">回复</el-link>
                        <el-link
                          class="b1"
                          :underline="false"
                          v-if="login!=null&&login.email==comment.user.email"
                          @click="remove(comment.uid)"
                        >删除</el-link>
                      </div>

                      <div class="rightCommentList">
                        <!-- 回复框-->
                        <div class="comment" :id="comment.uid">
                          <div class="commentBox">
                            <span class="left">
                              <img src="../../static/img/defaultAvatar.png" />
                            </span>
                            <span class="right">
                              <textarea
                                id="textpanel"
                                class="textArea"
                                placeholder="既然来了，那就留下些什么吧~"
                                v-model="replyValue"
                                @input="vaildCount"
                              ></textarea>
                            </span>
                          </div>
                          <div class="bottom">
                            <el-button
                              class="submit p2"
                              type="primary"
                              @click="subReply(comment)"
                            >发送回复</el-button>
                            <el-button
                              class="cancel p2"
                              type="info"
                              @click="handleCancle(comment)"
                            >取消回复</el-button>
                            <div class="emoji-panel-btn p2">
                              <img src="../../static/img/face_logo.png" />
                            </div>
                            <span class="allow p2">还能输入{{count}}个字符</span>
                          </div>
                        </div>
                      </div>

                      <!--回复渲染-->
                      <div>
                        <div v-for="reply in comment.replyList" :key="reply.uid">
                          <div class="commentList">
                            <span class="left p1">
                              <img src="../../static\img\touxiang.jpg" />
                            </span>
                          </div>

                          <span>
                            <div class="rightTop">
                              <el-link class="userName" :underline="false">{{reply.user.nickName}}</el-link>
                              <!-- <el-tag style="height: 30px; margin-left:5px;"></el-tag> -->
                              <span class="timeAgo" v-if="reply.createTime">{{(reply.createTime)}}</span>
                            </div>

                            <div class="rightCenter" v-html="reply.content"></div>
                            <div class="rightBottom">
                              <!-- <el-link class="b1" :underline="false" @click="replyTo(item)">回复</el-link> -->
                              <el-link
                                class="b1"
                                :underline="false"
                                v-if="login.email==comment.user.email"
                                @click="delComment(comment)"
                              >删除</el-link>
                            </div>
                            <div class="rightCommentList">
                              <!-- 回复框-->
                              <div class="comment" :id="comment.uid">
                                <div class="commentBox">
                                  <span class="left">
                                    <img src="../../static/img/defaultAvatar.png" />
                                  </span>
                                  <span class="right">
                                    <textarea
                                      id="textpanel"
                                      class="textArea"
                                      placeholder="既然来了，那就留下些什么吧~"
                                      v-model="replyValue"
                                      @input="vaildCount"
                                    ></textarea>
                                  </span>
                                </div>
                                <div class="bottom">
                                  <el-button
                                    class="submit p2"
                                    type="primary"
                                    @click="handleSubmit()"
                                  >发送回复</el-button>
                                  <el-button
                                    class="cancel p2"
                                    type="info"
                                    @click="handleCancle(comment)"
                                  >取消回复</el-button>
                                  <div class="emoji-panel-btn p2">
                                    <img src="../../static/img/face_logo.png" />
                                  </div>
                                  <span class="allow p2">还能输入{{count}}个字符</span>
                                </div>
                              </div>

                              <!--回复渲染-->
                            </div>
                          </span>
                        </div>
                      </div>
                    </span>
                  </div>
                </div>
              </div>
            </div>
          </ul>
        </div>
      </div>
    </div>

    <div class="sidebar">
      <Tag></Tag>
      <FollowMe></FollowMe>
      <SpecialRecommdendation></SpecialRecommdendation>
      <RecommdendationArticle></RecommdendationArticle>
    </div>
  </article>
</template>

<script>
import { getCreateTime } from "../util/dateUtil.js";
import Tag from "../components/Tag";
import FollowMe from "../components/FollowMe";
import SpecialRecommdendation from "../components/SpecialRecommdendation";
import RecommdendationArticle from "../components/RecommdendationArticle";
export default {
  data() {
    return {
      blogUid: 1,
      blog: {},
      blogs: [],
      value: "",
      count: 1024,
      login: {},
      replyValue: ""
    };
  },
  methods: {
    vaildCount() {
      var count = 1024 - this.value.length;
      if (count <= 0) {
        this.count = 0;
      } else {
        this.count = count;
      }
    },
    handleSubmit() {
      let session = window.localStorage;
      let login = JSON.parse(session.getItem("login"));
      if (login == null) {
        this.$message.error("登录后才可以进行评论");
        return;
      } else {
        let uid = 1;
        if (this.value == "") {
          this.$message.error("评论内容不可以为空");
          return;
        } else {
          let comments = this.blog.comments;
          uid =
            comments.length == 0
              ? 1
              : parseInt(comments[comments.length - 1].uid) + 1;
        }
        let comment = {
          uid,
          content: this.value,
          createTime: getCreateTime(),
          replyList: [],
          source: "info",
          user: login
        };
        this.blog.comments.push(comment);
        //TODO
        for (let i = 0; i < this.blogs.length; i++) {
          if (this.blogs[i].uid == this.blog.uid) {
            console.log("a");
            this.blogs[i] = this.blog;
            break;
          }
        }
        session.setItem("blogs", JSON.stringify(this.blogs));
        this.$message.success("发表评论成功");
        this.value = "";
      }
    },
    subReply(item) {
      let session = window.localStorage;
      let login = JSON.parse(session.getItem("login"));
      if (login == null) {
        this.$message.error("登录后才可以进行回复");
      } else {
        if (this.replyValue == "") {
          this.$message.error("回复内容不可以为空");
        } else {
          let uid = 1;
          if (this.blog.comments[item.uid - 1].replyList.length != 0) {
            uid =
              parseInt(
                this.blog.comments[item.uid-1].replyList[
                  this.blog.comments[item.uid - 1].replyList.length - 1
                ].uid
              ) + 1;
          }
          let reply = {
            uid,
            content: this.replyValue,
            createTime: getCreateTime(),
            replyList: [],
            source: "about",
            user: login,
            parent: item.uid
          };
          this.blog.comments[item.uid - 1].replyList.push(reply);
          this.blogs[this.blog.uid - 1] = this.blog;
          session.setItem("blogs", JSON.stringify(this.blogs));
          this.$message.success("发表回复成功");
          document.getElementById(item.uid).style.display = "block";
        }
      }
    },
    remove(uid) {
      this.$confirm("是否删除此评论", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          for (let i = 0; i < this.blog.comments.length; i++) {
            if (this.blog.comments[i].uid == uid) {
              this.blog.comments.splice(i, 1);
              break;
            }
          }
          for (let i = 0; i < this.blogs.length; i++) {
            if (this.blogUid == this.blogs[i].uid) {
              this.blogs[i] = this.blog;
              break;
            }
          }
          let session = window.localStorage;
          session.setItem("blogs", JSON.stringify(this.blogs));
          this.$message.success("删除成功");
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    replyTo: function(item) {
      if (this.login == null) {
        this.$message.error("登录后才可以进行评论");
        return;
      }
      let userUid = item.userUid;
      let commentUid = item.uid;
      var lists = document.getElementsByClassName("comment");
      for (var i = 0; i < lists.length; i++) {
        lists[i].style.display = "none";
      }
      document.getElementById(commentUid).style.display = "block";
      this.toInfo.commentUid = commentUid;
      this.toInfo.uid = userUid;
    },
    handleCancle(item) {
      let commentUid = item.uid;
      var lists = document.getElementsByClassName("comment");
      for (var i = 0; i < lists.length; i++) {
        lists[i].style.display = "none";
      }
      document.getElementById(commentUid).style.display = "none";
    }
  },
  components: {
    Tag,
    FollowMe,
    SpecialRecommdendation,
    RecommdendationArticle
  },
  created() {
    if (this.$route.query.blogUid) {
      this.blogUid = this.$route.query.blogUid;
    }
    let session = window.localStorage;
    let blogs = JSON.parse(session.getItem("blogs"));
    if (blogs == null) {
      this.$route.push({ path: "/" });
    }
    this.blogs = blogs;
    for (let i = 0; i < blogs.length; i++) {
      if (blogs[i].uid == this.blogUid) {
        this.blog = blogs[i];
        this.blog.clickCount += 1;
        this.blogs[i] = this.blog;
      }
    }
    this.login = JSON.parse(session.getItem("login"));
    session.setItem("blogs", JSON.stringify(this.blogs));
    if (this.login == null) this.login = {};
  }
};
</script>

<style>
.commentBox {
  min-width: 700px;
  width: 100%;
  height: 100px;
  margin: 0 auto;
}
.commentBox .left {
  display: inline-block;
  width: 4%;
  height: 100%;
  padding-top: 3px;
}
.commentBox .left img {
  cursor: pointer;
  margin: 0 auto;
  width: 90%;
  border-radius: 50%;
}
.commentBox .right {
  display: inline-block;
  margin: 5px 2px 0 0;
  width: 95%;
  height: 100%;
}
textarea::-webkit-input-placeholder {
  color: #909399;
}
.commentBox .right textarea {
  color: #606266;
  padding: 10px 5px 5px 10px;
  resize: none;
  width: 95%;
  height: 100%;
}
.bottom {
  position: relative;
  min-width: 690px;
  width: 98%;
  height: 60px;
  line-height: 40px;
  margin-top: 20px;
}
.bottom .p2 {
  float: right;
  margin-top: 5px;
  margin-right: 10px;
}
#bottom {
  position: relative;
  min-width: 690px;
  width: 98%;
  height: 60px;
  line-height: 40px;
  margin-top: 20px;
}
#bottom .p2 {
  float: right;
  margin-top: 5px;
  margin-right: 10px;
}
.emoji-panel-btn img {
  height: 35px;
  width: 35px;
}
.emoji-panel-btn:hover {
  cursor: pointer;
  opacity: 0.8;
}
.commentStyle {
  display: block;
  margin-top: 10px;
  margin-left: 10px;
  border-left: 1px dashed SlateGray;
}
.comment {
  display: none;
}
.commentList {
  width: 100%;
  margin: 0 auto;
}
.commentList .p1 {
  float: left;
}
.commentList .left {
  display: inline-block;
  width: 5%;
  height: 100%;
}
.commentList .left img {
  margin: 0 auto;
  width: 100%;
  border-radius: 50%;
}
.commentList .right {
  display: inline-block;
  width: 93%;
  margin-left: 5px;
}
.commentList .rightTop {
  height: 30px;
  margin-top: 2px;
}
.commentList .rightTop .userName {
  color: #303133;
  margin-left: 10px;
  font-size: 16px;
  font-weight: bold;
}
.commentList .rightTop .timeAgo {
  color: #909399;
  margin-left: 10px;
  font-size: 15px;
}
.commentList .rightCenter {
  margin-left: 20px;
  height: 50px;
  margin-top: 15px;
}
.commentList .rightBottom {
  margin-left: 10px;
  height: 30px;
}
.commentList .rightBottom el-link {
}
.commentList .rightBottom .b1 {
  margin-left: 10px;
}
</style>